/**
 * Copyright 2011 Mathieu Magnaudet
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.enac.geoapp.gui.custom.component;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

/**
 * 
 * @author mathieu
 * Custom component for selecting an item in a list
 */
@SuppressWarnings("serial")
public class RangeSelectorPanel extends JPanel {
	
	public RangeSelectorPanel(int[] itemList) {
		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		setLayout(gbl);
		setBackground(new Color(255,255,255,Color.TRANSLUCENT));
		
		ActionListener rangeListener = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				selectRange(((ImageLabelButton)e.getSource()).getValue());
			}

			
		};
		
		c.gridheight = 1;
		c.gridwidth = 1;
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1.0;
		c.weighty = 1.0;
		for(int i = 0; i < itemList.length;i++) {
			c.gridy = i;
			
			ImageLabelButton button = new ImageLabelButton("images/item_range_bg.png", itemList[i] + "NM");
			button.setBackground(new Color(255,255,255,Color.TRANSLUCENT));
			button.setRolloverIcon(new ImageIcon("images/item_range_bg_selected.png"));
			button.setValue(Integer.toString(itemList[i]));
			button.setForeground(Color.BLACK);
			button.setFont(new Font("SansSerif", Font.BOLD, 18));
			gbl.setConstraints(button, c);
			add(button);
			button.addActionListener(rangeListener);
		}
		c.gridy= itemList.length;
		ImageLabelButton button = new ImageLabelButton("images/up_arrow.png","");
		button.setValue("");
		gbl.setConstraints(button, c);
		add(button);
		button.addActionListener(rangeListener);
		
	}
	
	public  void selectRange(String newRange) {
		firePropertyChange("selectedRange", null, newRange);
	}
}
